package com.lzq.online_learning_system.comm;

import com.alibaba.fastjson.JSONObject;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @ClassName MyIntercepter
 * @Description TODO
 * @Author Administrator
 * @Date 2019/10/15 0015 10:38
 * @Version 1.0
 * 我的拦截器
 */
@Component
@Configuration
public class MyIntercepter /*implements WebMvcConfigurer*/ {

/*
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

   HandlerInterceptor interceptor = new HandlerInterceptor() {

            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                if (request.getMethod().equals("OPTIONS")) {
                    response.setStatus(HttpServletResponse.SC_OK);
                    return true;
                }
                response.setCharacterEncoding("utf-8");

                String token = request.getHeader("admin-token");

                if (token != null) {

                    boolean result = TokenUtil.verify(token);
                    if (result) {
                        System.out.println("通过拦截器");
                        return true;
                    }
                }

                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/json; charset=utf-8");
                PrintWriter out = null;
                try {
                    JSONObject json = new JSONObject();
                    json.put("success", "false");
                    json.put("msg", "认证失败，未通过拦截器"+request.getRequestURL());
                    json.put("code", "50000");
                    response.getWriter().append(json.toJSONString());
                    System.out.println("认证失败，未通过拦截器"+request.getRequestURL());
                    System.out.println("认证失败，未通过拦截器"+request.getSession());
                    //        response.getWriter().write("50000");
                } catch (Exception e) {
                    e.printStackTrace();
                    response.sendError(500);
                    return false;
                }

                return false;
            }

            @Override
            public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

            }

            @Override
            public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

            }

        };
        String url="/api/login";
        registry.addInterceptor(interceptor).addPathPatterns("/**")
                .excludePathPatterns("/css/**","/js/**","/images/**")
                .excludePathPatterns("/doc.html")
                .excludePathPatterns(url)


                .excludePathPatterns("index.html")
                .excludePathPatterns("/teResult/teacherCheck")
                .excludePathPatterns("/teResult/teacherClazz").
                excludePathPatterns("/webjars/**")
                .excludePathPatterns("/resource/**")
                .excludePathPatterns("/swagger-resources")
                .excludePathPatterns("/v2/**")
                .excludePathPatterns("/comm/**")

                .excludePathPatterns("/webjars/**")
                .excludePathPatterns("/Swagger-Bootstrap-UI/**").excludePathPatterns("/Gitee:https://gitee.com/xiaoym/**");


    }*/


}
